#include "head.h"
#include "cc9d_support.h"
#include "Pump.h"
#include "stm32f10x.h"

#define LOCAL_PORT Pump_PORT

#define GPIOx GPIOA
#define GPIO_Pin_n GPIO_Pin_8
#define RCC_APB2Periph_GPIOx RCC_APB2Periph_GPIOA

static inline void on() {
	GPIO_SetBits(GPIOx, GPIO_Pin_n);
}
static inline void off() {
	GPIO_ResetBits(GPIOx, GPIO_Pin_n);
}

static int on_tick = 0;

void Pump_recvPkg(sCc9dPkg * pkg, uint8 data_len) {
	if(data_len == 0){
		return;
	}
	switch (pkg->datas[0]) {
		case 'o': {
			if(data_len == 2){
				on_tick = pkg->datas[1] * 1000;
				on();
			}else if(data_len == 1){
				on_tick = 2000;
				on();
			}
		}break;
		case 'c': {
			on_tick = 0;
			off();
		}break;
	}
	
}

void Pump_msTick(void) {
	if(on_tick > 0){
		on_tick--;
		if(on_tick == 0){
			off();
		}
	}	
}

void Pump_init() {
	GPIO_InitTypeDef  GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOx, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_n;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOx, &GPIO_InitStructure);
	off();
}

